﻿using ctolua.Models.System;
using ctolua.Models.Type;

namespace ctolua.Models
{
    /// <summary>
    /// 技能
    /// </summary>
    public class MAbility
    {
        /// <summary>
        /// 技能对象
        /// </summary>
        public Ability ability;
        /// <summary>
        /// 自定义内容
        /// </summary>
        public Dictionary<dynamic, dynamic> diykv = new Dictionary<dynamic, dynamic>();
        /// <summary>
        /// 技能ID
        /// </summary>
        public string id => global_api.ability_to_str(this.ability);
        /// <summary>
        /// Key
        /// </summary>
        public int? key => this.ability.api_get_ability_id();
        public MAbility(Ability ability)
        {
            this.ability = ability;
        }



        /// <summary>
        /// 获取技能icon图标的图片id
        /// </summary>
        /// <returns>图片id</returns>
        public int? icon => game_api.get_icon_id_by_ability_type(this.key);
        /// <summary>
        /// 返回指定技能类型的描述
        /// </summary>
        /// <returns>描述</returns>
        public string desc => game_api.get_ability_desc_by_type(this.key);
        /// <summary>
        /// 返回指定技能类型的名称
        /// </summary>
        /// <returns>名称</returns>
        public string name => ability.api_get_name();
        /// <summary>
        /// 技能类型是否受冷却缩减影响
        /// </summary>
        /// <returns>布尔值</returns>
        public bool? is_cd_reduce => game_api.api_get_influenced_by_cd_reduce(this.key);
        /// <summary>
        /// 获取技能最大等级
        /// </summary>
        /// <returns>最大等级</returns>
        public int? max_level => game_api.get_ability_max_level(this.ability);

        /// <summary>
        /// 技能实体是否存在
        /// </summary>
        /// <returns>布尔值</returns>
        public bool? is_null => game_api.ability_is_exist(this.ability);
        /// <summary>
        /// 冷却时间
        /// </summary>
        public string cd => ability.api_get_str_attr(TABILITY_FLOAT_ATTRS.冷却时间);
        /// <summary>
        /// 蓝耗
        /// </summary>
        public string cost => ability.api_get_str_attr(TABILITY_FLOAT_ATTRS.魔法消耗);
        /// <summary>
        /// 技能拥有者单位
        /// </summary>
        public MUnit owner_unit => MUnit.obj(ability.api_get_owner());
        /// <summary>
        /// 
        /// </summary>
        public int 长度 { 
            get=> this.get_ability_conf_formula_attr(TABILITY_FORMULA_ATTRS.长度);
            set => this.ability.api_set_ability_arrow_length(py.Fix32(value) );
        }
            
        /// <summary>
        /// 转对象 带缓存
        /// </summary>
        /// <param name="u">单位对象</param>
        /// <returns></returns>
        public static MAbility obj(Ability a)
        {
            string unit = py.toString(a);
            MAbility mUnit = SCache.getAbilityCache<MAbility>(unit);
            if (mUnit == null)
            {
                mUnit = new MAbility(a);
                SCache.setAbilityCache(unit, mUnit);
                return mUnit;
            }
            return mUnit;
        }

        /// <summary>
        /// 技能选取的目标单位
        /// </summary>
        /// <param name="runtime_id ">  runtime_id   None</param>
        /// <returns>单位</returns>
        public Unit get_target_unit(int? runtime_id) => game_api.get_target_unit_in_ability(this.ability, runtime_id);

        /// <summary>
        /// 技能选取的目标物品
        /// </summary>
        /// <param name="runtime_id ">  runtime_id   None</param>
        /// <returns>物品</returns>
        public Item get_target_item(int? runtime_id) => game_api.get_target_item_in_ability(this.ability, runtime_id);
        /// <summary>
        /// 技能选取的目标可破坏物
        /// </summary>
        /// <param name="runtime_id ">  runtime_id   None</param>
        /// <returns>可破坏物</returns>
        public Destructible get_target_dest(int? runtime_id) => game_api.get_target_dest_in_ability(this.ability, runtime_id);
        /// <summary>
        /// 获取技能释放的位置
        /// </summary>
        /// <param name="runtime_id"> runtime_id </param>
        /// <returns>技能释放的位置</returns>
        public Point get_release_position(int? runtime_id)=> this.ability.api_get_release_position( runtime_id);
        /// <summary>
        /// 技能类型的公式属性
        /// </summary>
        /// <param name="attr_name"> 公式属性 </param>
        /// <param name="level"> 当前等级 </param>
        /// <param name="stack_count"> 当前充能层数 </param>
        /// <param name="unit"> 施法计算单位 </param>
        public int get_ability_conf_formula_attr( string attr_name, int? level=1, int? stack_count = 1)
        {
            return game_api.get_ability_conf_formula_attr_with_unit(this.key, attr_name, level, stack_count,this.owner_unit.unit,null).@int();
        }
        
    }
}
